﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace zPod.TweetMyFace
{
	public static class UserPreferencesHelper
	{
		private const string ApplicationName = "TweetMyFace";
		private const string ConfigFileName = "config.dat";

		public static void SavePreferences(Dictionary<string, string> preferences)
		{
			var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

			var dir = Path.Combine(appData, ApplicationName);

			if (!Directory.Exists(dir))
			{
				Directory.CreateDirectory(dir);
			}

			var config = Path.Combine(dir, ConfigFileName);

			using (StreamWriter writer = new StreamWriter(config))
			{
				foreach (var kvp in preferences)
				{
					writer.WriteLine(kvp.Key);
					writer.WriteLine(kvp.Value);
				}
			}
		}

		public static Dictionary<string, string> LoadPreferences()
		{
			var preferences = new Dictionary<string, string>();

			var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

			var dir = Path.Combine(appData, ApplicationName);

			if (!Directory.Exists(dir))
			{
				return preferences;
			}

			var config = Path.Combine(dir, ConfigFileName);

			if (!File.Exists(config))
			{
				return preferences;
			}

			using (StreamReader reader = new StreamReader(config))
			{
				while (!reader.EndOfStream)
				{
					preferences.Add(reader.ReadLine(), reader.ReadLine());
				}
			}

			return preferences;
		}
	}
}
